package org.example.managementsystem.controller;

import org.example.managementsystem.model.ProviderComplaint;
import org.example.managementsystem.model.ProviderComplaintReview;
import org.example.managementsystem.service.ProviderComplaintService;
import org.example.managementsystem.service.ProviderComplaintReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供应商投诉与审核表现层
 * @author 王振祎
 * @date 2025-05-21
 */
@RestController
@RequestMapping("/api/provider/complaints")
public class ProviderComplaintController {
    private final ProviderComplaintService providerComplaintService;
    private final ProviderComplaintReviewService providerComplaintReviewService;

    @Autowired
    public ProviderComplaintController(ProviderComplaintService providerComplaintService, ProviderComplaintReviewService providerComplaintReviewService) {
        this.providerComplaintService = providerComplaintService;
        this.providerComplaintReviewService = providerComplaintReviewService;
    }

    /**
     * 提交供应商投诉
     */
    @PostMapping("/submit")
    public ResponseEntity<String> submitComplaint(@RequestBody ProviderComplaint complaint) {
        // 保证数据安全，设置申请状态为待审核
        complaint.setApproved(false);
        complaint.setStatus(false);
        providerComplaintService.submitComplaint(complaint);
        return ResponseEntity.ok("供应商投诉提交成功");
    }

    /**
     * 审核供应商投诉并记录审核信息
     */
    @PutMapping("/review")
    public ResponseEntity<String> reviewComplaint(@RequestBody ProviderComplaintReview review) {
        providerComplaintReviewService.reviewComplaint(review);
        return ResponseEntity.ok("供应商投诉审核成功");
    }

    /**
     * 更新投诉信息
     */
    @PutMapping("/update")
    public ResponseEntity<String> updateComplaint(@RequestBody ProviderComplaint complaint) {
        providerComplaintService.updateComplaint(complaint);
        return ResponseEntity.ok("供应商投诉更新成功");
    }

    /**
     * 获取所有投诉
     */
    @GetMapping("/getComplaint")
    public ResponseEntity<List<ProviderComplaint>> getAllComplaints() {
        List<ProviderComplaint> complaints = providerComplaintService.getAllComplaints();
        return ResponseEntity.ok(complaints);
    }

    /**
     * 获取所有未审核的投诉
     */
    @GetMapping("/getUnreviewedComplaint")
    public ResponseEntity<List<ProviderComplaint>> getAllUnreviewedComplaints() {
        List<ProviderComplaint> complaints = providerComplaintService.getAllUnreviewedComplaints();
        return ResponseEntity.ok(complaints);
    }

    /**
     * 根据ID获取投诉
     */
    @GetMapping("/getComplaintById/{complaintId}")
    public ResponseEntity<ProviderComplaint> getComplaintById(@PathVariable int complaintId) {
        ProviderComplaint complaint = providerComplaintService.getComplaintById(complaintId);
        return ResponseEntity.ok(complaint);
    }

    /**
     * 根据ID获取审核记录
     */
    @GetMapping("/getReviewById/{complaintId}")
    public ResponseEntity<ProviderComplaintReview> getReviewById(@PathVariable int complaintId) {
        ProviderComplaintReview review = providerComplaintReviewService.getReviewById(complaintId);
        return ResponseEntity.ok(review);
    }

    /**
     * 根据ID删除投诉
     */
    @DeleteMapping("/delete/{complaintId}")
    public ResponseEntity<String> deleteComplaint(@PathVariable int complaintId) {
        providerComplaintService.deleteComplaint(complaintId);
        return ResponseEntity.ok("供应商投诉删除成功");
    }

}